package com.aos.erp.schools.service.student.imp;

import com.aos.erp.common.bean.PageBean;
import com.aos.erp.common.dao.mapper.Condition;
import com.aos.erp.schools.mapper.student.CoachStudentMapper;
import com.aos.erp.schools.mapper.student.StudentExamMapper;
import com.aos.erp.schools.model.schoolclass.DriversSchoolClassInfoVo;
import com.aos.erp.schools.model.student.CoachStudent;
import com.aos.erp.schools.model.student.Student;
import com.aos.erp.schools.model.student.StudentExam;
import com.aos.erp.schools.model.student.StudentExamHistory;
import com.aos.erp.schools.model.student.ro.exam.StudentYueKaoRo;
import com.aos.erp.schools.model.student.ro.exam.UpdateYueKaoRo;
import com.aos.erp.schools.model.student.ro.student.SelectByStateRo;
import com.aos.erp.schools.model.student.vo.eaxm.ShowUpdateExamVo;
import com.aos.erp.schools.model.student.vo.eaxm.StudentYueKaoExamVo;
import com.aos.erp.schools.model.student.vo.student.StudentEaxmVo;
import com.aos.erp.schools.service.student.CoachStudentService;
import com.aos.erp.schools.service.student.StudentExamHistoryService;
import com.aos.erp.schools.service.student.StudentExamService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Auther: chb
 * @Date: 2018/11/2 14:48
 * @Description: 学员预约考试Serviceimp
 */
@Service
public class StudentExamServiceImpl extends ServiceImpl<StudentExamMapper, StudentExam> implements StudentExamService {



    @Override
    public void saveBean(StudentExam studentExam) throws Exception {

        //1, 如果有数据 就修改  没有就 添加
        Condition condition = new Condition(StudentExam.class);
        if (studentExam.getStudentId()!=null && studentExam.getExamSubject()!=null ) {
            condition.eqFiled("studentId", studentExam.getStudentId());
            condition.eqFiled("examSubject", studentExam.getExamSubject());
        }
        StudentExam oldStudent=this.getOne(condition.getWrapper());

        if(oldStudent==null || oldStudent.getId()==null)
        {
            studentExam.setDegree(1L);//第一次
            studentExam.setCreated(new Date());
            this.save(studentExam);
        }else {
            studentExam.setId(oldStudent.getId());
            studentExam.setCreated(oldStudent.getCreated());
            studentExam.setUpdated(new Date());
            studentExam.setDegree(oldStudent.getDegree()+1L);//记录次数
            this.updateById(studentExam);
         }
    }

    @Override
    public PageBean<StudentEaxmVo> queryBySubject(SelectByStateRo ro) {
        Page<StudentEaxmVo> page = new Page<StudentEaxmVo>(ro.getPageIndex(), ro.getPageSize());
        List<StudentEaxmVo> noticeInfoList = baseMapper.queryBySubject(page, ro);
        page.setRecords(noticeInfoList);
        PageBean<StudentEaxmVo> bean = new PageBean<StudentEaxmVo>(page.getCurrent(), page.getSize(), page.getTotal());
        bean.setOptimizeCountSql(page.optimizeCountSql());
        bean.setAscs(page.ascs());
        bean.setDescs(page.descs());
        bean.setRecords(page.getRecords());
        bean.condition();
        return bean;
    }

    @Override
    public PageBean<StudentYueKaoExamVo> queryStudentYueKaoExam(StudentYueKaoRo ro) {
        Page<StudentYueKaoExamVo> page = new Page<StudentYueKaoExamVo>(ro.getPageIndex(), ro.getPageSize());
        List<StudentYueKaoExamVo> noticeInfoList = baseMapper.queryStudentYueKaoExam(page, ro);
        page.setRecords(noticeInfoList);
        PageBean<StudentYueKaoExamVo> bean = new PageBean<StudentYueKaoExamVo>(page.getCurrent(), page.getSize(), page.getTotal());
        bean.setOptimizeCountSql(page.optimizeCountSql());
        bean.setAscs(page.ascs());
        bean.setDescs(page.descs());
        bean.setRecords(page.getRecords());
        bean.condition();
        return bean;
    }

    @Override
    public int updateYueKaoExam(UpdateYueKaoRo ro) {
        return baseMapper.updateYueKaoExam(ro);
    }

    @Override
    public PageBean<StudentEaxmVo> queryStudentYueKao(SelectByStateRo ro) {
        Page<StudentEaxmVo> page = new Page<StudentEaxmVo>(ro.getPageIndex(), ro.getPageSize());
        List<StudentEaxmVo> noticeInfoList = baseMapper.queryStudentYueKao(page, ro);
        page.setRecords(noticeInfoList);
        PageBean<StudentEaxmVo> bean = new PageBean<StudentEaxmVo>(page.getCurrent(), page.getSize(), page.getTotal());
        bean.setOptimizeCountSql(page.optimizeCountSql());
        bean.setAscs(page.ascs());
        bean.setDescs(page.descs());
        bean.setRecords(page.getRecords());
        bean.condition();
        return bean;
    }

    @Override
    public ShowUpdateExamVo showUpdateExam(long studentId) {
        return baseMapper.showUpdateExam(studentId);
    }

    @Override
    public int saveUpdateEaxmData(StudentExam ro) {
        return baseMapper.saveUpdateEaxmData(ro);
    }

    @Override
    public StudentExam selectOne(long id) {
        return baseMapper.selectById(id);
    }


}
